#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2021/6/11 10:50
# @Author  : fuchen'en
# @Description:

import pika

credentials = pika.PlainCredentials('test', 'test123')
parameters = pika.ConnectionParameters(host='10.0.20.66',
                                       port='5672',
                                       virtual_host='/',
                                       credentials=credentials)
connection = pika.BlockingConnection(parameters)
channel = connection.channel()

channel.exchange_declare(exchange='logs', exchange_type='fanout')

# 通过传空名的队列，让MQ随机生成队列名，随机生成的队列在结果中获取
# exclusive标记每个消费者独享一个队列
result = channel.queue_declare(queue='', exclusive=True)
queue_name = result.method.queue

# 队列要绑定到交换机上，以便交换机分发消息
channel.queue_bind(exchange='logs', queue=queue_name)

print(' [*] Waiting for logs. To exit press CTRL+C')


def callback(ch, method, properties, body):
    print(" [x] {!r}".format(body))


channel.basic_consume(queue=queue_name, on_message_callback=callback, auto_ack=True)

channel.start_consuming()
